package com.zhiyou.app.shop.website.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.IDicItemDao;
import com.zhiyou.app.shop.website.dao.IEpComplainDao;
import com.zhiyou.app.shop.website.dao.IEpListDao;
import com.zhiyou.app.shop.website.dao.IMoneyDealDao;
import com.zhiyou.app.shop.website.dao.IMsgDao;
import com.zhiyou.app.shop.website.dao.IUserAccountDao;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.EpComplain;
import com.zhiyou.app.shop.website.dao.pojo.EpList;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.dao.pojo.Msg;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IEpListService;
import com.zhiyou.app.shop.website.service.IUserAccountService;
import com.zhiyou.app.shop.website.service.IUserManagerService;

@Service(value="epListService")
public class EpListServiceImpl implements IEpListService {

	@Autowired
	private IEpListDao epListDao;
	@Autowired
	private IUserAccountDao userAccountDao;
	@Autowired
	private IUserInfoDao userInfoDao;
	@Autowired
	private IMsgDao msgDao;
	@Autowired
	private IDicItemService dicItemService;
	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private IUserManagerService userManagerService;
	@Autowired
	private IMoneyDealDao moneyDealDao;
	@Autowired
	private IEpComplainDao epComplainDao;

	public void saveEpTrade(String account, EpList ep) {
		List<UserAccount> list = userAccountDao.findByProperty("userId", account);
		if(list != null && list.size() > 0){
			UserAccount userAccount = list.get(0);
			userAccount.setBonusMoney(userAccount.getBonusMoney().subtract(ep.getNeedAmont())); //冻结中
			userAccountDao.update(userAccount);
		}
		epListDao.save(ep);
	}

	public List findOnSale(String status, String account, int startNum,int pagesize) {
		return epListDao.findEpSale(status,account, startNum, pagesize);
		
	}
	
	public Integer findOnSaleCnt(String status, String account){
		return epListDao.findEpSaleCnt(status, account);
	}

	public EpList findbyId(String id) {
		return epListDao.findById(id);
	}

	public void update(EpList ep) {
		epListDao.update(ep);
	}

	public void saveEp(User user, EpList ep, String isNotice) {
		Date date = new Date();
		Timestamp time = new Timestamp(date.getTime());
		userInfoDao.update(user);
		ep.setDealDate(time);
		epListDao.update(ep);
		if("1".equals(isNotice)){  //给卖方发购买通知，并注意收款
			Integer deadtime = 0;
			List<DicItem> dicItems = dicItemService.findByType(ShopConstants.EP_TIME);
			if(dicItems != null && dicItems.size() > 0){
				deadtime = new Integer(dicItems.get(0).getCode());
			}
			Msg msgsale = new Msg();//卖方邮件
			msgsale.setCreateDate(time);
			msgsale.setFlag(ShopConstants.TRUE);
			msgsale.setTitle("EP交易买方通知");
			msgsale.setType("1");
			msgsale.setReplyId("");
			msgsale.setReceiveId(ep.getUserId());
			msgsale.setSendId("客服");
			msgsale.setMsg("您好，用户"+user.getUserAccount()+"通过ep交易购买了您"+ep.getDealAmount()+"个咖啡豆。请注意收款确认");
			msgDao.save(msgsale);
			Msg buy = new Msg();  //买方邮件 通知及时汇款
			buy.setCreateDate(time);
			buy.setFlag(ShopConstants.TRUE);
			buy.setTitle("EP交易成功，及时汇款通知");
			buy.setType("1");
			buy.setReplyId("");
			buy.setReceiveId(ep.getRelationId());
			buy.setSendId("客服");
			buy.setIsRead(ShopConstants.FALSE);
			buy.setMsg("您好，您已成功购买来自卖方用户"+
			ep.getUserId()+"的"+ep.getDealAmount()+"个咖啡豆，请在"+new Timestamp(date.getTime()+(deadtime*60*60*1000))
					+"之前把对应款项汇入对方账户中,并在系统的【EP交易】-->【查看个人交易明细】中点击【汇款】按钮" +
					"通知对方确认收款，如果汇款之后对方未及时确认，可以写邮件箱客服投诉，" +
					"若您六小时之内未汇款，则视为主动放弃交易，并扣除相应的信用度");
			msgDao.save(buy);
			
		}
	}

	public List findepbyuser(String account,int startNum,int pagesize) {
		
		return epListDao.findepByuser(account, startNum, pagesize);
	}

	public Integer findepCntbyuser(String account) {
		return epListDao.findepCntByuser(account);
	}

	public void epRemit(EpList ep) { //买方已汇款，提醒卖方收款
		epListDao.update(ep);
		Msg msg = new Msg();
		msg.setCreateDate(new Timestamp(new Date().getTime()));
		msg.setFlag(ShopConstants.TRUE);
		msg.setIsRead(ShopConstants.FALSE);
		msg.setMsg("您好，用户"+ep.getRelationId()+"已经将ep交易中对应的款项汇入您的账户，请注意查收，" +
				"若收到汇款请及时在【EP交易】-->【查看个人交易明细】中点击【确认收款】，若未收到款项，您可以给客服写邮件投诉该用户");
		msg.setReceiveId(ep.getUserId());
		msg.setSendId(ep.getRelationId());
		msg.setTitle("EP交易确认收款通知");
		msg.setType("1");
		msgDao.save(msg);
		
	}

	public void epTradeSuc(EpList ep) {
		Timestamp date = new Timestamp(new Date().getTime());
		User sale = userManagerService.getUserByAccount(ep.getUserId());
		User buy = userManagerService.getUserByAccount(ep.getRelationId());
		ep.setStatus("3");
		ep.setSuccessDate(date);
		epListDao.update(ep);
		UserAccount account = userAccountService.findByUserAccount(ep.getRelationId());       //买方账户加上奖金币
		account.setBonusMoney(account.getBonusMoney().add(ep.getDealAmount()));
		userAccountDao.update(account);
		//卖房 减去奖金币 交易金额
		MoneyDeal deal1 = new MoneyDeal();
		deal1.setAccountSubject("EP交易卖出");
		deal1.setRemark("EP交易卖出给"+buy.getUserAccount());
		deal1.setAccountType(ShopConstants.PRIZE);
		deal1.setCreateDate(date);
		deal1.setPay(ep.getDealAmount());
		deal1.setSubjectType("4");
		deal1.setUserId(sale.getUserId());
		moneyDealDao.save(deal1);
		//卖方 减去 手续费
		MoneyDeal deal2 = new MoneyDeal();
		deal2.setAccountSubject("EP交易手续费");
		deal2.setAccountType(ShopConstants.PRIZE);
		deal2.setCreateDate(date);
		deal2.setPay(ep.getChargeFee());
		deal2.setSubjectType("4");
		deal2.setUserId(sale.getUserId());
		deal2.setRemark(buy.getUserAccount());
		moneyDealDao.save(deal2);
		//卖方减去  汇款费
		MoneyDeal deal3 = new MoneyDeal();
		deal3.setAccountSubject("EP交易汇款费");
		deal3.setAccountType(ShopConstants.PRIZE);
		deal3.setCreateDate(date);
		deal3.setPay(ep.getPostFee());
		deal3.setSubjectType("4");
		deal3.setUserId(sale.getUserId());
		deal3.setRemark(buy.getUserAccount());
		moneyDealDao.save(deal3);
		//买方加上奖金币
		MoneyDeal deal4 = new MoneyDeal();
		deal4.setAccountSubject("EP交易买入");
		deal4.setAccountType(ShopConstants.PRIZE);
		deal4.setCreateDate(date);
		deal4.setIncome(ep.getDealAmount());
		deal4.setSubjectType("4");
		deal4.setUserId(buy.getUserId());
		deal4.setRemark(sale.getUserAccount());
		moneyDealDao.save(deal4);
		//买房加上汇款费
		MoneyDeal deal5 = new MoneyDeal();
		deal5.setAccountSubject("EP交易汇款费");
		deal5.setAccountType(ShopConstants.PRIZE);
		deal5.setCreateDate(date);
		deal5.setIncome(ep.getPostFee());
		deal5.setSubjectType("4");
		deal5.setUserId(buy.getUserId());
		deal5.setRemark(sale.getUserAccount());
		moneyDealDao.save(deal5);
		
	}

	public List<EpList> findEpTrading() {
		EpList ep = new EpList();
		ep.setStatus("2");
		return epListDao.findByExample(ep, Order.asc("dealDate"));
	}

	public void findEp(Page page, EpList ep) {
		if(ep == null)
			ep = new EpList();
		epListDao.findByExample(page, ep, Order.desc("createDate"));
		
	}

	public void delete(EpList ep) {
		epListDao.delete(ep);
	}

	public void epCancel(String id) {
		EpList ep = epListDao.findById(id);
		List<UserAccount> list = userAccountDao.findByProperty("userId", ep.getUserId());
		if(list != null && list.size() > 0){
			UserAccount userAccount = list.get(0);
			userAccount.setBonusMoney(userAccount.getBonusMoney().add(ep.getNeedAmont())); //解 冻结
			userAccountDao.update(userAccount);
		}
		epListDao.delete(ep);
		
	}

	public List findEpList(String buy, String sale, int startNum, int pagesize) {
		return epListDao.findEpList(buy, sale, startNum, pagesize);
	}

	public Integer findEpListCnt(String buy, String sale) {
		return epListDao.findEpList(buy, sale);
	}

	public void saveEpComplain(EpList ep, EpComplain epComplain) {
		epListDao.save(ep);
		epComplainDao.save(epComplain);
	}
	
/*	public static void main(String[] args) {
		System.out.println(new Timestamp(new Date().getTime()+(6*60*60*1000)));
	}*/
}
